<html>
<head><meta charset="utf-8"><title>linked-list traversal · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/linked-list.20traversal.html">linked-list traversal</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="147639813"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/linked-list%20traversal/near/147639813" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/linked-list.20traversal.html#147639813">(Nov 14 2018 at 01:52)</a>:</h4>
<p>If I have a linked list, is it safe to have mutable references to two distinct nodes at the same time? That is, could I have</p>
<div class="codehilite"><pre><span></span>A -&gt; B -&gt; C -&gt; D
</pre></div>


<p>Keeping mutable references to [<code>A</code>, <code>C</code>], then increment to have [<code>A</code>, <code>D</code>] then [<code>B</code>, <code>D</code>]?</p>



<a name="147639878"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/linked-list%20traversal/near/147639878" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/linked-list.20traversal.html#147639878">(Nov 14 2018 at 01:54)</a>:</h4>
<p>Or is the fact that <code>C</code> is reachable from <code>A</code> enough to declare this UB? If that's the case, do I just need to to keep both as raw pointers and only dereference for exceedingly short spans of time?</p>



<a name="147640142"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/linked-list%20traversal/near/147640142" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gankra <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/linked-list.20traversal.html#147640142">(Nov 14 2018 at 02:01)</a>:</h4>
<p>the current guidelines are that reachability isn’t transitive through references, but also your links are raw pointers so it’s super fine</p>



<a name="147640983"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/linked-list%20traversal/near/147640983" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/linked-list.20traversal.html#147640983">(Nov 14 2018 at 02:23)</a>:</h4>
<blockquote>
<p>but also your links are raw pointers</p>
</blockquote>
<p>Why do you say that? Assuming it's some form of <code>Box</code> which thus has a raw pointer somewhere in it?</p>



<a name="147655425"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/linked-list%20traversal/near/147655425" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/linked-list.20traversal.html#147655425">(Nov 14 2018 at 09:04)</a>:</h4>
<p>The current guidelines don't say much.^^ But, <span class="user-mention" data-user-id="116155">@Jake Goulding</span> could you just write some little testcase doing it and send me the code? (The miri on playground unfortunately doesn't implement the latest version of Stacked Borrows, still waiting for a rustc PR to land.)</p>



<a name="147655500"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/linked-list%20traversal/near/147655500" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/linked-list.20traversal.html#147655500">(Nov 14 2018 at 09:06)</a>:</h4>
<p>Stacked Borrows says: The important part is that <code>&amp;mut</code> is unique, i.e. between two uses (where use is defined very liberally, and includes assignment -- i.e. , in <code>let x = y</code>, <code>y</code> gets "used"), there must not have been any access to the memory this reference points to, except through "derived" references -- i.e., references created from the reference in question with reborrowing.</p>



<a name="147676010"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/linked-list%20traversal/near/147676010" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/linked-list.20traversal.html#147676010">(Nov 14 2018 at 15:18)</a>:</h4>
<p>I'd be happy to write a testcase, as soon as I wrangle NLL into submission...</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>